//
//  ProductInfoViewController.m
//  iShop
//
//  Created by svp on 19/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ProductInfoViewController.h"
#import "SubProduct.h"
#import "ResourceLoader.h"
#import "TextViewCell.h"
#import "BaseProduct.h"
#import "CustomSlideCell.h"
#import "TitleTextfieldCell.h"
#import "Brand.h"

enum EProductInfoSection {
	EBrand,
	ECustomerRank,
	EUserRank,
	EColor_Size,
	EDescription,
	EProductInfoSectionCount
};

@implementation ProductInfoViewController

@synthesize tableView = _tableView;
@synthesize shop = _shop;
@synthesize product = _product;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	NSLog(@"ProductInfoViewController dealloc");
	[_tableView release];
    [super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return EProductInfoSectionCount;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	switch (section) {
		case ECustomerRank:
			return [NSString stringWithFormat:@"Rank of %@ Customers",_product.BaseProduct.BaseRankCount];
		case EUserRank:
			return @"User Rank";
		case EColor_Size:
			return @"Color/Size";
		case EDescription:
			return @"Description";
		default:
			return @"";
	}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	switch (section) {
		case EColor_Size:
			return _subProducts.count;
		case EDescription:
			return 1;
		case EBrand:
			return 2;

		default:
			return 1;
	}
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
	switch (indexPath.section) {
		case EColor_Size:
			return 44;
		case EDescription:
			return 210;
		default:
			return 44;
	}
}

-(void) customSlideCellchangeValue:(UITableViewCell*)cell
{
	NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
	if (indexPath.section == 2) {
		_currentValue = ((CustomSlideCell*)cell).slide.value;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (indexPath.section == 0) {
		TitleTextfieldCell* cell = (TitleTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:@"TitleTextfieldCell"];
		if (!cell) {
			ResourceLoader *rc = [[[ResourceLoader alloc] initWithNibNamed:@"TitleTextfieldCell"] autorelease];
			cell = (TitleTextfieldCell*)rc.resource;
		}
		cell.titleLabel.text = indexPath.row == 0 ? @"Brand Name" : @"Brand Origin"; 
		cell.textField.text = indexPath.row == 0 ? _product.BaseProduct.Brand.BrandName : _product.BaseProduct.Brand.BrandOrigin;
		return cell;
	}
	if (indexPath.section < 3 ) {
		CustomSlideCell* cell = (CustomSlideCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomSlideCell"];
		if (!cell) {
			ResourceLoader *rc = [[[ResourceLoader alloc] initWithNibNamed:@"CustomSlideCell"] autorelease];
			cell = (CustomSlideCell*)rc.resource;
			cell.delegate = self;			
		}
		NSLog(@"test:%@",_product.BaseProduct);
		cell.slide.enabled = indexPath.section == 2;
		cell.slide.value = indexPath.section == 1?[_product.BaseProduct.BaseRankAVG floatValue]: _currentValue;
		cell.selectionStyle = indexPath.section == 1 ? UITableViewCellSelectionStyleNone: UITableViewCellSelectionStyleBlue;
		[cell slideChange:nil];
		return cell;
		
	}
	switch (indexPath.section) {
		case EColor_Size:
		{
			UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ColorSizeCell"];
			if (!cell) {
				NSLog(@"Create cell");
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ColorSizeCell"] autorelease];
			}
			cell.imageView.backgroundColor = [UIColor redColor];
			cell.textLabel.text = ((SubProduct*)[_subProducts objectAtIndex:indexPath.row]).ColorAndSize;
			cell.detailTextLabel.text =[NSString stringWithFormat:@"Amount: %@",[((SubProduct*)[_subProducts objectAtIndex:indexPath.row]).Amount stringValue]];
			return cell;
		}
		case EDescription:
		{
			TextViewCell *cell = (TextViewCell*)[tableView dequeueReusableCellWithIdentifier:@"TextViewCell"];
			if (!cell) {
				NSLog(@"Create cell");
				ResourceLoader *rc = [[[ResourceLoader alloc] initWithNibNamed:@"TextViewCell"] autorelease];
				cell = (TextViewCell*)rc.resource;		
				cell.textView.text = @"Description";
			}
			cell.textView.text = _product.BaseProduct.BaseDescription;
			return cell;
		}
		default:
			return nil;
	}
}

-(IBAction) closePress:(id) sender;
{
	NSLog(@"cancelPress");
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(void) prepareData
{
	_subProducts = [[_product.SubProducts allObjects] retain];
	_currentValue = 5.0;
}


@end
